// pages/transfer_application/transfer_application.js
var app = getApp();
var formdata={};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_value:'',
    orderid :'',
    ID: '', 
    imgs: []
  
  },
  // 上传图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 9) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 1) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        // console.log(imgs);
        that.setData({
          imgs: imgs
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  changeinput:function(e){
    var that = this;
    that.setData({
      input_value: e.detail.value
    })
  },
  submitInput1:function(e){
    var that = this;
    if (that.data.input_value == '') {
     
      wx.showToast({
        title: '请填写原因',
        icon: 'none',
        mask: true,
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
      return false
    }
    console.log(that.data.imgs)
    if (that.data.imgs.length <=0) {
     
      wx.showToast({
        title: '请选择图片',
        icon: 'none',
        mask: true,
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
      return false
    }
    wx.showLoading({
      title: '提交中',
    })
    var that = this;
    console.log(e.detail.value)
    formdata = e.detail.value;
    formdata.openid = app.globalData.opid;
    formdata.orderid = that.data.orderid;
    console.log(formdata)
    console.log( that.data.imgs[0])
    wx.uploadFile({
      url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=shenqingtuihuo',
      filePath: that.data.imgs[0],
      name: 'pic',
      formData: formdata,
      header: {
        'content-type': 'multipart/form-data'
      }, 
      success:function(res){
        console.log(res)
        var data = JSON.parse(res.data)
        wx.hideLoading()
        if(data.success == 1){
          
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            mask: true,
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '../progress/progress?id=' + that.data.orderid
            }, 1500)
          })
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            mask: true,
          })
          setTimeout(function () {
            wx.navigateBack()
          },1000)
        }
      },
      
    })

  },
  submitInput:function(){
    var that = this;
    if (that.data.input_value == ''){
      wx.showLoading({
        title: '请填写原因',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
      return false
    }
    console.log(that.data.imgs)
    if (that.data.imgs) {
      wx.showLoading({
        title: '请选择图片',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
      return false
    }

    wx.request({
      url: 'https://rt.gzziyu.com//mobile/',
      data:{
        openid:app.globalData.opid,
        orderid: that.data.orderid,
        content:that.data.input_value,
        pic: that.data.imgs[0]
      },
     
      success:function(res){
        console.log(app.globalData.opid)
        console.log(that.data.orderid)
        console.log(that.data.input_value)
        console.log(that.data.imgs[0])
        console.log(that.data.orderid, that.data.input_value, that.data.imgs)
        wx.showLoading({
          title: '提交中',
        })
        if (res.data.success == 1 || res.data.success == "1"){
         
            wx.hideLoading()
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              mask: true,
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '../progress/progress?id=' + that.data.orderid
              },1500)
            })
           
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            mask: true,
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options)
    that.setData({
      orderid:options.orderid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.gotoHomePage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})